function toThousand(number, tenThousand = true) {
  const nextNumber = number ? `${number}` : '-';
  if (!number || nextNumber === '' || nextNumber === '-') return '-';
  if (nextNumber.indexOf(',') > -1) return nextNumber;
  if (tenThousand && number - 0 > 10000) {
    return toTenThousand(number);
  }
  if (nextNumber === '' || nextNumber === '-') return '-';
  return nextNumber.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
}

// 数字绝对值值超过百万，则以万为单位
function toTenThousand(number) {
  const nextNumber = number ? `${number}` : '-';
  if (!number || nextNumber === '' || nextNumber === '-') return '-';
  let lastNumber = nextNumber.replace(/,/g, '');
  lastNumber = Number(lastNumber) / 10000;
  // lastNumber = lastNumber.toFixed(1);
  lastNumber = parseInt(lastNumber * 10) / 10;
  if (Number((lastNumber - 0).toFixed(0)) === Number(lastNumber)) {
    lastNumber = Number(lastNumber).toFixed(0);
  }
  if (lastNumber > 10000) {
    lastNumber = Number(lastNumber) / 10000;
    // lastNumber = lastNumber.toFixed(1);
    lastNumber = parseInt(lastNumber * 10) / 10;
    if (Number((lastNumber - 0).toFixed(0)) === Number(lastNumber)) {
      lastNumber = Number(lastNumber).toFixed(0);
    }
    if (lastNumber > 1000) lastNumber = lastNumber.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
    return `${lastNumber}亿`;
  } else if (lastNumber > 1000) lastNumber = lastNumber.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
  return `${lastNumber}万`;
}

function toPercent(number) {
  const nextNumber = number ? `${number}` : '-';
  if (nextNumber === '' || nextNumber === '-' || nextNumber === 'null') return '-';
  return `${nextNumber}%`;
}

function capitalized(word) {
  return word.toLowerCase().replace(/^([a-z])/, match => match.toUpperCase());
}

function isJSON(str) {
	if (typeof str == 'string') {
    try {
      const obj = JSON.parse(str);
      if (typeof obj == 'object' && obj) {
        return true;
      } else {
        return false;
      }
    } catch(e) {
      console.log('error：' + str + '!!!' + e);
      return false;
    }
	}
	console.log('It is not a string!');
  return false;
}

export default {
	toThousand,
	toTenThousand,
	toPercent,
	capitalized,
  isJSON
}
